package com.example.controller;

import com.example.exception.BusinessException;
import com.example.vo.LotteryResult;
import com.example.services.LotteryService;
import com.example.vo.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/lottery")
public class LotteryController {
    
    @Autowired
    private LotteryService lotteryService;
    
    @PostMapping("/draw")
    public Result<LotteryResult> draw(@RequestParam Long activityId, HttpServletRequest request) {
        // 获取当前用户ID (实际项目中从认证信息中获取)
        Long userId = getCurrentUserId();
        String ip = request.getRemoteAddr();
        
        try {
            LotteryResult result = lotteryService.draw(userId, activityId, ip);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.fail(e.getMessage());
        }
    }
    
    private Long getCurrentUserId() {
        // 实现获取当前用户ID的逻辑
        return 10001L;
    }

}
